﻿(function () {
  'use strict';
  var controllerId = 'projects';
  angular.module('ng4model').controller(controllerId, ['common', '$scope', '$modal', 'datacontext', projects]);

  function projects(common, $scope, $modal, datacontext) {
    var getLogFn = common.logger.getLogFn;
    var log = getLogFn(controllerId);
    // View Model
    var vm = this;
    vm.title = 'Projects'; 
    vm.projectsPublished = {};
    vm.projectsOpen = {};
    vm.projectsNeedHelp = {};

    activate();

    function activate() {
      var promises = [getProjects(), getProjectsPublished()];
      common.activateController(promises, controllerId)
          .then(function () { log('Activated Projects View'); });
    }

    // Data
    function getProjectsPublished() {
      return datacontext.getProjectsPublished(true).then(function (data) {
        return vm.projectsPublished = data;
      });
    }

    function getProjects() {
      return datacontext.getProjects(false).then(function (data) {
        return vm.projectsOpen = data;
      });
    }


    // Modal Forms
    $scope.open = function() {

      var dialogInstance = $modal.open({
        templateUrl: 'newProject.html',
        controller: newProject
      });

      dialogInstance.result.then(function(selectedItem) {
        $scope.selected = selectedItem;
      }, function() {
        log.info('Dialog closes');
      });

    };

    var newProject = function($dialogInstance, items) {

      $scope.items = items;
      $scope.selected = {
        item: $scope.items[0]
      };
      // user can click
      $scope.ok = function() {
        $dialogInstance.close('create');
      };
      $scope.sample = function() {
        $dialogInstance.close('sample');
      };
      // user can click
      $scope.cancel = function() {
        $dialogInstance.dismiss('cancel');
      };

    };


  }
})();